package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.model.Dept;
import org.xqyy.eval.model.TDept;
import org.xqyy.eval.service.IDeptService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-24.
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;


    @RequestMapping("/loadDept")
    @ResponseBody
    public Map<String, Object> loadDept(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        String pcode = request.getParameter("pcode");
        if (StringUtils.hasLength(pcode)) {
            res.put("pcode", pcode);
        }
        String groupCode = request.getParameter("groupCode");
        if (StringUtils.hasLength(groupCode)) {
            res.put("groupCode", groupCode);
        }
        String status = request.getParameter("status");
        if (StringUtils.hasLength(status)) {
            res.put("status", status);
        }
        List<Map<String, Object>> data = deptService.load(res);
        res.put("root", data);
        return res;
    }

    @RequestMapping("/addDept")
    @ResponseBody
    public Map<String, Object> addDept(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String leader = request.getParameter("leader");
            String pcode = request.getParameter("pcode");
            String groupCode = request.getParameter("groupCode");
            res.put("code", code);
            res.put("name", name);
            res.put("leader", leader);
            res.put("status", true);
            res.put("pcode", pcode);
            res.put("groupCode", groupCode);
            deptService.add(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/updateDept")
    @ResponseBody
    public Map<String, Object> updateDept(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            String name = request.getParameter("name");
            String leader = request.getParameter("leader");
            String status = request.getParameter("status");
            String pcode = request.getParameter("pcode");
            String groupCode = request.getParameter("groupCode");
            res.put("code", code);
            if (StringUtils.hasLength(name)) {
                res.put("name", name);
            }
            if (StringUtils.hasLength(leader)) {
                res.put("leader", leader);
            }
            if (StringUtils.hasLength(status)) {
                res.put("status", Boolean.valueOf(status));
            }
            if (StringUtils.hasLength(pcode)) {
                res.put("pcode", pcode);
            }
            if (StringUtils.hasLength(groupCode)) {
                res.put("groupCode", groupCode);
            }
            deptService.modify(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/deleteDept")
    @ResponseBody
    public Map<String, Object> deleteDept(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String code = request.getParameter("code");
            res.put("code", code);
            deptService.delete(res);
            res.put("success",true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/selectAllDept")
    @ResponseBody
    public List<TDept> selectAllDept(HttpServletRequest request, HttpServletResponse response) {
        return deptService.selectAllDept();
    }

    @RequestMapping("/selectAllTDept")
    @ResponseBody
    public List<TDept> selectAllTDept() {
        return deptService.selectAllTDept();
    }

    @RequestMapping("/selectAllTDeptIsEnable")
    @ResponseBody
    public List<TDept> selectAllTDeptIsEnable() {
        return deptService.selectAllTDeptIsEnable();
    }

    @RequestMapping("/addTDept")
    @ResponseBody
    public Boolean addTDept(TDept tDept) {
        try {
            deptService.addTDept(tDept);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/updateTDept")
    @ResponseBody
    public Boolean updateTDept(TDept tDept) {
        try {
            deptService.updateTDept(tDept);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/findTDeptById")
    @ResponseBody
    public TDept findTDeptById(TDept tDept) {

        return deptService.findTDeptById(tDept);
    }

    @RequestMapping("/convertTDept")
    @ResponseBody
    public Boolean convertTDept(TDept tDept) {
        try {
            deptService.convertTDept(tDept);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
